<?php

namespace app\api\service;

use app\common\model\Product;
use app\common\model\ShoppingCart;
use app\common\model\User;
use app\Response;

class ShoppingCartService
{
    /*** 购物车列表
     * @param $user_id int
     * @param $page int
     * @param $num int
     * @return json
    */
    public static function dataListService($user_id,$page,$num,$shi_type){
        $userInfo = User::userInfo(["uid"=>$user_id]);
        if(!in_array($userInfo['type'],[22,23])){
            return Response::wrapError(0,"账户非买家用户!");
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError(-200,"此账户已被禁用,如有疑问请联系平台客服!");
        }
        $where[] = ["s.enable","=",1];
        $where[] = ["s.uid","=",$user_id];
        $where[] = ["s.state","=",1];
        $where[] = ["s.shi_type","=",$shi_type];
        $where[] = ["p.state","=",5];
        $list = ShoppingCart::shopCartList($where,$page,$num);
        if(!empty($list)){
            $data = $list->toArray();
            foreach ($data as $k => $v){
                $data[$k]["addtime"] = !empty($v["addtime"])?date("Y-m-d H:i:s",$v["addtime"]):"";
            }
            $count = ShoppingCart::shopCartListCount($where);
        }else{
            $data = [];
            $count = 0;
        }
        return Response::wrapData($data,$count);
    }


    /*** 添加购物车
     * @param $user_id int
     * @param $page int
     * @param $num int
     * @return json
     */
    public static function increaseDataService($user_id,$p_id,$p_name,$num){
        $userInfo = User::userInfo(["uid"=>$user_id]);
        if(!in_array($userInfo['type'],[22,23])){
            return Response::wrapError(0,"账户非买家用户!");
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError(-200,"此账户已被禁用,如有疑问请联系平台客服!");
        }
        $p_info = Product::dataInfo(["id"=>$p_id,"enable"=>1]);
        if(empty($p_info)){
            return Response::wrapError(0,"商品信息异常");
        }
        if($p_info["state"] != 5){
            return Response::wrapError(0,"商品已下架");
        }
        $s_info = ShoppingCart::dataInfo(["uid"=>$user_id,"p_id"=>$p_id]);
        $data = [
            "uid" => $user_id,
            "p_id" => $p_id,
            "p_name" => $p_name,
            "num" => $num,
            "addtime" => time(),
            "state" => 1,
            "enable" => 1,
            "shi_type" => $p_info["shi_type"]
        ];
        if(!empty($s_info)){
            if($s_info["state"] == 2||$s_info["enable"] == 0){
                $res = ShoppingCart::editInfo(["id"=>$s_info["id"]],$data);
            }else{
                $res = ShoppingCart::incInfo(["id"=>$s_info["id"]],"num",(int)$num);
            }
        }else{
            $res = ShoppingCart::addInfo($data);
        }
        if ($res){
            return Response::wrapData();
        }else{
            return Response::wrapError(0,"网络异常,请稍后重试!");
        }
    }

    /*** 移出购物车
     * @param $user_id int
     * @param $id int
     * @return json
     */
    public static function removeDataService($user_id,$id){
        $userInfo = User::userInfo(["uid"=>$user_id]);
        if(!in_array($userInfo['type'],[22,23])){
            return Response::wrapError(0,"账户非买家用户!");
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError(-200,"此账户已被禁用,如有疑问请联系平台客服!");
        }
        $s_info = ShoppingCart::dataInfo(["uid"=>$user_id,"id"=>$id]);
        if(empty($s_info)){
            return Response::wrapData();
        }else{
            ShoppingCart::editInfo(["id"=>$s_info["id"]],["enable"=>0]);
        }
        return Response::wrapData();
    }

    /*** 修改商品数量
     * @param $user_id int
     * @param $page int
     * @param $num int
     * @return json
     */
    public static function editNumService($user_id,$id,$type,$num){
        $userInfo = User::userInfo(["uid"=>$user_id]);
        if(!in_array($userInfo['type'],[22,23])){
            return Response::wrapError(0,"账户非买家用户!");
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError(-200,"此账户已被禁用,如有疑问请联系平台客服!");
        }
        $s_info = ShoppingCart::dataInfo(["uid"=>$user_id,"id"=>$id]);
        if($type == 1){
            $res = ShoppingCart::incInfo(["id"=>$s_info["id"]],"num",(int)$num);
        }elseif($type == 2){
            $res = ShoppingCart::decInfo(["id"=>$s_info["id"]],"num",(int)$num);
        }elseif($type == 3){
            if($num < 1){
                return Response::wrapError(0,"商品数量不能改为0或负数!");
            }
            if($s_info["num"] == $num){
                return Response::wrapData();
            }
            $res = ShoppingCart::editInfo(["id"=>$s_info["id"]],["num"=>(int)$num]);
        }
        if ($res){
            return Response::wrapData();
        }else{
            return Response::wrapError(0,"网络异常,请稍后重试!");
        }
    }
}